package com.lch.base.http.transformer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lch.base.http.model.params.JsonParam;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 数据解析器
 *
 * @author lee(29487113@qq.com) on 18/2/4
 */
public class TransformerGson implements ITransformer {
    public static final String ENCODING = "UTF-8";
    private final static Gson sGson = new Gson();

    @Override
    public <T> T transformer(Response<ResponseBody> response, Type type) throws Exception {
        if (response.body() == null) {
            return null;
        }

        try {
            if (type.equals(Bitmap.class)) {
                return (T) BitmapFactory.decodeStream(response.body().byteStream());
            } else if (type.equals(JSONObject.class)) {
                byte[] data = response.body().bytes();
                if (data != null) {
                    JSONObject result = (JSONObject) new JSONTokener(new String(data, ENCODING)).nextValue();
                    return (T) result;
                }

            } else if (type.equals(JSONArray.class)) {
                byte[] data = response.body().bytes();
                if (data != null) {
                    JSONArray result = (JSONArray) new JSONTokener(new String(data, ENCODING)).nextValue();
                    return (T) result;
                }
            } else if (type.equals(String.class)) {
                byte[] data = response.body().bytes();
                if (data != null) {
                    String result = new String(data, ENCODING);
                    return (T) result;
                }
            } else if (type.equals(byte[].class)) {
                return (T) response.body().bytes();
            }

//        else if (type.equals(File.class)) {
//            File file = new File(FILE_CACHE_DIR, Md5Util.makeMD5(response.request().url().toString()));
//            File parentDir = file.getParentFile();
//            if (!parentDir.exists()) {
//                parentDir.mkdirs();
//            }
//            if (file.exists()) {
//                file.delete();
//            }
//            try {
//                Buffer buffer = new Buffer();
//                buffer.readFrom(response.body().byteStream());
//                file.createNewFile();
//                OutputStream os = new FileOutputStream(file);
//                buffer.writeTo(os);
//
//                os.flush();
//                os.close();
//                buffer.flush();
//                buffer.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            return (T) file;
//        }

            else if (type.equals(InputStream.class)) {
                return (T) response.body().byteStream();
            } else {
                Reader reader = response.body().charStream();
                T object = sGson.fromJson(reader, type);
                return object;
            }
        } catch (Exception e) {
            Log.e("", "transform Exception: " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }


//    /**
//     * http请求cache
//     */
//    public static String CACHEDIR = Environment.getExternalStorageDirectory()
//            + "/Android/data/" + BaseApplication.getAppContext().getPackageName()
//            + "/okhttpCache";
//
//    /**
//     * 下载文件存储路径
//     */
//    public String FILE_CACHE_DIR = CACHEDIR + "/file";


    @Override
    public String jsonString(JsonParam jsonParam) {
        // TODO: 18/2/4 是否需要处理---------
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.addSerializationExclusionStrategy(new IgnoreStrategy());
        return gsonBuilder.create().toJson(jsonParam);
    }

}
